﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.Data;

using JUtil;
using System.Reflection;

[DataObject(true)]
public class TheDataObjectOperator : DatabaseDataObjectOperatorBase
{
    #region Generate SQL
    
    private string getSelectSQL()
    {
        string sql = "SELECT * FROM CUSTOMERS";
        return sql;
    }

    private string getUpdateSQL(TheDataObject aRec)
    {
        string sql = "UPDATE CUSTOMERS SET CompanyName = '{0}' WHERE CustomerID = '{1}'";
        sql = string.Format(sql, aRec.CompanyName, aRec.CustomerID);
        return sql;
    }

    private string getDeleteSQL(TheDataObject aRec)
    {
        string sql = "DELETE FROM CUSTOMERS WHERE CustomerID = '{0}'";
        sql = string.Format(sql, aRec.CustomerID);
        return sql;
    }

    #endregion

    #region DataObjectMethod
    
    [DataObjectMethod(DataObjectMethodType.Select, true)]
    public DataTable Select(int startRowIndex, int maximumRows)
    {
        DataTable dt = SelectSQL(getSelectSQL());
        var pagedData = dt.AsEnumerable().Skip(startRowIndex).Take(maximumRows);
        return pagedData.CopyToDataTable();
    }

    [DataObjectMethod(DataObjectMethodType.Insert, false)]
    public void Insert(TheDataObject aRec) { 
        throw new NotImplementedException(); 
    }

    [DataObjectMethod(DataObjectMethodType.Update, false)]
    public void Update(TheDataObject aRec) {
        string sql = getUpdateSQL(aRec);
        ExecuteSQL(sql);
    }

    [DataObjectMethod(DataObjectMethodType.Delete, false)]
    public void Delete(TheDataObject aRec) {
        string sql = getDeleteSQL(aRec);
        ExecuteSQL(sql);
    }

    public int SelectCount(int startRowIndex, int maximumRows)
    {
        return SelectCountSQL(getSelectSQL());
    }

    #endregion

} // end of TheDataObjectOperator
